package operation.android.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * String工具类 处理大驼峰,小驼峰,下划线字段转换
 */
public class StringUtil {
    /**
     * 下划线转小驼峰  hello_world == helloWorld
     */
    public static String LineToHump(String str) {
        Pattern linePattern = Pattern.compile("_(\\w)");
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 下划线转大驼峰 hello_world == HelloWorld
     */
    public static String LineToBigHump(String str) {
        String s = LineToHump(str);
        return s.substring(0, 1).toUpperCase() + s.substring(1);
    }

    /**
     * 小驼峰转下划线 helloWorld == hello_world
     */
    public static String HumpToLine(String str) {
        String res = "";
        char[] charArray = str.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] >= 'A' && charArray[i] <= 'Z') {
                res += "_" + charArray[i];
            } else {
                res += charArray[i];
            }
        }
        return res.toLowerCase();
    }

    /**
     * 大驼峰转下划线 HelloWorld == hello_world
     */
    public static String BigHumpToLine(String str){
        return HumpToLine(Hump(str));
    }

    /**
     * 驼峰转换 HelloWorld == helloWorld || helloWorld == HelloWorld
     */
    public static String Hump(String str){
        char first = str.charAt(0);
        if(first >= 'A' && first <= 'Z'){
            first += 32;
        } else {
            first -= 32;
        }
        return String.valueOf(first) + str.substring(1);
    }

    /**
     * 下划线转中横线 HelloWorld == hello-world
     */
    public static String BigHumpToOne (String str) {
        String Str = BigHumpToLine(str);
        return Str.replace("_", "-");
    }
}
